//
//  LXDelayApplyViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/20.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXDelayApplyViewController.h"
#import "LXTableViewCell.h"
#import "KeHuInfoModel.h"
#import "LXRemarkTabelViewCell.h"
#import "LXDelayModel.h"
#import "LXChooseCustomViewController.h"



@interface LXDelayApplyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSArray *data;
@property (nonatomic) NSMutableDictionary *loanDic;
@property (nonatomic) NSDictionary *selDic;

@property (nonatomic) NSString *customerId;
@end

@implementation LXDelayApplyViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
  
   
     
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
    if (self.type==1) {
        [self layoutCustomNav:self.view title:@"新增延期记录"];
    }
    else {
        [self layoutCustomNav:self.view title:@"修改延期记录"];
        
    }
    
    [self.tableView registerClass:[LXTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LXTableViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXRemarkTabelViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
    

    
    [self.view addSubview:self.tableView];
    
    [self loadData];
    
    if (self.type==1) {
        [self setBottomViewWithText:@"确认新增"];
    }
    else {
        [self setBottomViewWithText:@"确认修改"];
    }
    
    
    
}
- (void)loadData{
    
    self.loanDic  = [NSMutableDictionary dictionary];
    
    
    
    // 模型为空,说明从车辆详情进入
    if (!ObjectIsEmpty(self.delayModel)) {
        
        // 车牌号
        [self.loanDic setValue:self.delayModel.carNumber forKey:@"carNumber"];
        
     
        // 修改的时候需要带入数据
        if (self.type==2) {
            
            self.customerId = self.delayModel.customerId;
            [self.loanDic setValue:self.delayModel.customerName forKey:@"customerName"];
            // 延期天数
            [self.loanDic setValue:self.delayModel.delayDay forKey:@"delayDay"];
            // 备注
            [self.loanDic setValue:self.delayModel.remark forKey:@"remark"];
        }
        
    }
    
    

    
    self.data = @[
           @{
               @"title":@"车牌号",
               @"key":@"carNumber",
               @"placeHolder" : @"请输入",
               @"canEdit":@(NO),
               @"showAccess":@(NO),
               @"maxLength":@(1000),
               @"sel":@"",
               @"height":@(kComHeight)
           },
           @{
               @"title":@"*客户姓名",
               @"key":@"customerName",
               @"placeHolder" : @"请选择客户",
               @"canEdit":@(NO),
               @"showAccess":self.type==1?@(YES):@(NO),
               @"maxLength":@(20),
               @"sel":self.type==1?@"clickCustomerName":@"",
               @"height":@(kComHeight)
           },
           @{
               @"title":@"*延期天数(天)",
               @"key":@"delayDay",
               @"placeHolder" : @"请输入延期天数",
               @"canEdit":self.type==1?@(YES):@(NO),
               @"showAccess":@(NO),
               @"maxLength":@(3),
               @"keyboardType":@(UIKeyboardTypeNumberPad),
               @"inputType":@(NUM),
               @"sel":@"",
               @"height":@(kComHeight)
           },
           @{
               @"title":@"备注",
               @"key":@"remark",
               @"placeHolder" : @"请输入~",
               @"height":@(150)
           },
         
           
    ];
    
   
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.data count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.data[indexPath.row];
    return [dic[@"height"] doubleValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *dic = self.data[indexPath.row];

    
    if([dic[@"key"] isEqualToString:@"remark"]){
        LXRemarkTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
        cell.textView.placeholder = dic[@"placeHolder"];
        cell.textView.maxLength = 200;
        cell.remarkTitle.text = dic[@"title"];
        cell.textView.text = self.loanDic[dic[@"key"]];
        
        [cell setTextViewEdit:^(NSString * _Nonnull textStr) {
            self.loanDic[dic[@"key"]]  = textStr;
        }];
        return cell;
    }
    
    
    LXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setClipsToBounds:YES];
    

    cell.cellDic = dic;
    cell.textFiled.hidden = NO;
    cell.subTitleLab.hidden = YES;
    cell.textFiled.text = self.loanDic[dic[@"key"]];
    
    
    cell.textfiledChangeBlock = ^(NSString * _Nonnull textStr) {
        self.loanDic[dic[@"key"]]  = textStr;
    };
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic = self.data[indexPath.row];
    self.selDic = dic;
    NSString * selName = dic[@"sel"];
    SEL action = NSSelectorFromString(selName);
    if ([self respondsToSelector:action]) {
        [self performSelector:action withObject:nil afterDelay:0];
    }

}

#pragma mark -- 选择客户
-(void)clickCustomerName{
    LXChooseCustomViewController *vc = [[LXChooseCustomViewController alloc] init];
    [vc setToSendValue:^(KeHuInfoModel * _Nonnull model) {
        [self.loanDic setValue: [NSString stringWithFormat:@"%@",model.customerName]  forKey:@"customerName"];
        self.customerId = model.ID;
        [self.tableView reloadData];
    }];
    [self pushToNextController:vc];
}
- (NSArray *)data{
    if (!_data) {
        _data = [[NSArray alloc]init];
    }
    return _data;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, ScreenHeight - kViewTopHeight - kComHeight - 5) style:UITableViewStylePlain];
        _tableView.backgroundColor = ColorWithF7F8FA;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}


#pragma mark ---- 确认新增/修改点击事件
- (void)clickBottomView:(UIButton *)sender{
    

    
    if (StringIsEmpty(self.loanDic[@"carNumber"])) {
        showAlert(@"请选择车牌号");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"customerName"])) {
        showAlert(@"请选择客户");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"delayDay"])) {
        showAlert(@"请输入延期天数");
        return;
    }
    if ([self.loanDic[@"delayDay"] intValue] == 0) {
        showAlert(@"延期天数不能小于1天");
        return;
    }
    
    NSMutableDictionary *dic = @{
        @"carId":self.delayModel.carId, // 所属车辆id
        @"carNumber":self.delayModel.carNumber,// 所属车辆车牌号
        @"customerName":self.loanDic[@"customerName"],// 客户
        @"customerId":self.customerId,// 客户id
        @"delayDay":self.loanDic[@"delayDay"],// 逾期天数
        @"remark":self.loanDic[@"remark"],// 逾期天数
        
    }.mutableCopy;
    
    NSString *url;
    
    if (self.type==1) {
        url = [NSString stringWithFormat:@"%@",POST_PostAddTsigncontractdelay];
    }
    
    else if (self.type==2) {
        url = [NSString stringWithFormat:@"%@",POST_PostUpdateTsigncontractdelay];
        [dic setValue:self.delayModel.ID forKey:@"id"];
    }

    
    [Tool showAlertTitle:@"是否确认提交" msg:nil cancelTitle:@"取消" commitBtnTitle:@"确定" cancelHandlerBlock:^{
    } commitHandlerBlock:^{
        [MSNetworkManager postRevertUrl:url params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
            if (isSuccess) {
                if (self.type==1) {
                    showAlert(@"新增延期成功");
                }
                else if (self.type==2) {
                    showAlert(@"修改延期成功");
                }
                
                // carModel 有值说明从详情页进入,返回时须刷新列表
                if (!ObjectIsEmpty(self.delayModel)) {
                    kUserDefaultSetBool(YES, kRefreshStr);
                }
                [self.navigationController popViewControllerAnimated:YES];
            }
           
        } failure:^(NSUInteger code, NSString * _Nonnull message) {
        } withShowHud:YES];
    } onVC:self];
    
   
   
    
    
    
}



@end
